package com.kadron.session;

import javax.security.auth.login.LoginException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.Startup;
import org.jboss.seam.annotations.intercept.BypassInterceptors;
import org.jboss.seam.security.Credentials;
import org.jboss.seam.security.Identity;

@Name("org.jboss.seam.security.identity") 
@Scope(ScopeType.SESSION)
@BypassInterceptors 
@Startup 
public class CustomIdentity extends Identity {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5905153285631330583L;

	private static final Log log = LogFactory.getLog(CustomIdentity.class);

	@In
	private Credentials credentials;

	@Override
	public String login() {
		try {
			authenticate();
			if (log.isDebugEnabled()) {
				log.debug("Login successful for: " + credentials.getUsername());
			}
			return "loggedIn";
		}

		catch (LoginException ex) {
			if (log.isDebugEnabled()) {
				log.debug("Login failed for: " + credentials.getUsername(), ex);
			}
			return null;
		}
	}

}
